package com.zhangxiaolong.controller;


import com.zhangxiaolong.pojo.Result;
import com.zhangxiaolong.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {


    @Autowired
    private AliOSSUtils aliOSSUtils;
/*    @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws Exception {
        log.info("文件上传:{},{},{}",username,age,image);
        //E:\learn\code\java\tliaswebmagament\image
        String originalFilename = image.getOriginalFilename();

        //构造唯一的文件名
        int index = originalFilename.lastIndexOf(".");
        String extname = originalFilename.substring(index);
        String newFileName = UUID.randomUUID().toString() + extname;
        log.info("新的文件名：{}",newFileName);
        image.transferTo(new File("E:\\learn\\code\\java\\tliaswebmagament\\image\\" + newFileName));
        return Result.success();
    }*/

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("图片路径:{}",image.getOriginalFilename());
        String url = aliOSSUtils.upload(image);
        return Result.success(url);
    }
}
